package edu.szte.ovrt.datahandler.core;

import java.io.*;

import org.junit.*;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.*;

import static org.powermock.api.easymock.PowerMock.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(AbstractIOFactory.class)
public class AbstractIOFactoryTest {
	private static final String FILE_NAME = "fileName.extension";
	private AbstractIOFactory m_factory;

	@Before
	public void createFactoryMock() {
		m_factory = createPartialMock(AbstractIOFactory.class, "createDocumentReader", "createDocumentWriter");
	}

	@After
	public void verifyFactoryMock() {
		verify(m_factory);
	}

	@Test
	public void testKeyPropertyDefault() {
		replay(m_factory);

		final String fileName = m_factory.getFileName();

		assertNull(fileName);
	}

	@Test
	public void testKeyPropertyGetSet() {
		replay(m_factory);

		m_factory.setFileName(FILE_NAME);
		final String actualFileName = m_factory.getFileName();

		assertEquals(FILE_NAME, actualFileName);
	}

	@Test
	public void testCreateFileWriter() throws Exception {
		m_factory.setFileName(FILE_NAME);
		final PrintWriter mockWriter = createMockAndExpectNew(PrintWriter.class, FILE_NAME);
		replay(PrintWriter.class, mockWriter, m_factory);

		final Writer writer = m_factory.createFileWriter();

		assertSame(mockWriter, writer);
	}

	@Test
	public void testCreateFileReader() throws Exception {
		m_factory.setFileName(FILE_NAME);
		final FileReader mockReader = createMockAndExpectNew(FileReader.class, FILE_NAME);
		replay(FileReader.class, mockReader, m_factory);

		final Reader reader = m_factory.createFileReader();

		assertSame(mockReader, reader);
	}
}
